/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shapes;

import utilities.Util;

/**
 *
 * @author ctuchsch
 */
public class Square
{

    public final int numPosVertices = 4;
    public final int numColorVertices = 4;
    public final int numSTVertices = 2;
    public final int sides = 1;
    private final float x;
    private final float y;
    private final float z;
    private final float width;
    private final float height;
    private int orientation = 0;
    private TexturedVertex[] vertices = new TexturedVertex[4];
    private byte[] order = new byte[]{0, 1, 3, 3, 1, 2};

    public Square(float x, float y, float z, float width, float height, int orientation)
    {
        this.x = x;
        this.y = y;
        this.z = z;
        this.height = height;
        this.width = width;
        this.orientation = orientation;
        setUpVertex();
    }

    private void setUpVertex()
    {
        if (orientation == 0)
            flatSquare();
        else
            uprightSquare();
    }
//don't like this it's messy
    public float[] toFloatArray()
    {
       return Util.ConcatArrayf(vertices[0].getElements(),vertices[1].getElements(),vertices[2].getElements(),vertices[3].getElements());
    }

    public TexturedVertex[] toTexturedVert()
    {
        return vertices;
    }

    private void flatSquare()
    {
        float halfWidth = width / 2;
        float halfDepth = height / 2;
        //bottom left
        vertices[0] = new TexturedVertex()
                .setXYZ((x - halfWidth), y, (z + halfDepth))
                .setST(0, 1);
        //bottom right
        vertices[1] = new TexturedVertex()
                .setXYZ((x + halfWidth), y, (z + halfDepth))
                .setST(1, 1);
        //top right
        vertices[2] = new TexturedVertex()
                .setXYZ((x + halfWidth), y, (z - halfDepth))
                .setST(1, 0);
        //top left
        vertices[3] = new TexturedVertex()
                .setXYZ((x - halfWidth), y, (z - halfDepth))
                .setST(0, 0);
    }

    private void uprightSquare()
    {
        float halfWidth = width / 2;
        float halfHeight = height / 2;

        //bottom left
        vertices[0] = new TexturedVertex()
                .setXYZ((x - halfWidth), (y - halfHeight), z)
                .setST(0, 1);
        //bottom right
        vertices[1] = new TexturedVertex()
                .setXYZ((x + halfWidth), (y - halfHeight), z)
                .setST(1, 1);
        //top right
        vertices[2] = new TexturedVertex()
                .setXYZ((x + halfWidth), (y + halfHeight), z)
                .setST(1, 0);
        //top left
        vertices[3] = new TexturedVertex()
                .setXYZ((x - halfWidth), (y + halfHeight), z)
                .setST(0, 0);
    }

    public byte[] getIndexBuffer()
    {
        return order;
    }

    public int getNumVerts()
    {
        int out = (numPosVertices + numColorVertices + numSTVertices) * vertices.length;
        return out;
    }
    
    public int getIndicesCount()
    {
        return order.length;
    }
    
}
